<?xml version="1.0" encoding="windows-1255" ?>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="dataBase.magaers.CodeDBMngr" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%! public static Boolean visibility; %>
<script type="text/javascript">
var vis;
var jsCid;
var jsVersion;
function init(visibile, cid, version){
	vis = visibile;
	jsCid = cid;
	jsVersion = version;
}
function editCode(codeBody){
	//TODO: send the code as CDATA
	var codeDiv = document.getElementById("code");
	codeDiv.innerHTML =	"<form name=\"up\"><textarea name=\"code\" cols=\"40\" rows=\"10\" >"+
		codeBody + "</textarea><br />"+
	"<input type=\"submit\" name=\"upload\" value=\"upload\" onclick=\"return uploadCode(this.form)\" /></form>";
	return false;	
}

function uploadCode(uploadForm)
{
	var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("POST", "../editCode?edit=edit&cid="+jsCid+"&version="+jsVersion, false);
	xmlHttp.setRequestHeader("Content-type", "text/plain");
	xmlHttp.setRequestHeader("Content-length", uploadForm.code.value.length);
	xmlHttp.send(uploadForm.code.value);
	var codeDiv = document.getElementById("code");
	codeDiv.innerHTML = xmlHttp.responseText;
	return false;
}

function vibilityChange(){
	var visibililtyDiv = document.getElementById("visibility");

	if (vis == 'true'){
		vis = 'false';
	} else {
		vis = 'true';
	}	
	visibililtyDiv.innerHTML = "Visibility: " + vis;
	return vis;
	
}
function changeVisibility(){
	var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("POST", "../editCode?edit=vis&cid="+jsCid+"&version="+jsVersion, false);
	xmlHttp.send(null);
   	return vibilityChange();
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
<title>Code Displaying</title>
<%	String cid = request.getParameter("cid");
	String version = request.getParameter("version");
	String email = (String) session.getAttribute("email");
	CodeDBMngr cdbm = CodeDBMngr.getInstance();
	if ((cid == null) || (version == null)){
		response.sendError(400);
		return;
	}	
	boolean allowence = cdbm.getAllowence(cid, version, email);
	if (cdbm == null){
		response.sendError(500);
		return;
	}
	String body = cdbm.searchCodeByCidVersion(cid, version);
	if (body == null){
		response.sendError(500);
		return;
	}%>
	<% visibility = Boolean.valueOf(cdbm.isVisible(cid, version));
	if ((visibility == null)) {
		response.sendError(500);
		return;
	} 	%>
</head>
<body onload="init('<% out.print(visibility.toString());%>', '<% out.print(cid);%>', '<% out.print(version);%>')">
<% String link = request.getParameter("link");
   if (link != null && link.equals("search")) {%>
		<p><a href="search.html">new search</a></p><br />
	<% } else { %>
		<p><a href="xslt.jsp">new xslt</a></p><br /> <% } %>
	<% if (allowence) { %>
	<table>
		<tr>
			<td><form name="codeEditing" >
				<input type="button" name="edit" value="edit" onclick="return editCode(<pre><code><% out.print(body); %></code></pre>)" />
			</form></td>
			<td><form name="codeEditing" action="../editCode?edit=delete&amp;cid=<%out.print(cid);%>&amp;version=<%out.print(version);%>" method="post" >
				<input type="submit" name="delete" value="delete" />
			</form></td>
			<td><form name="codeEditing"  >
				<input type="button" name="change visibility" value="change visibility" onclick="return changeVisibility()" />
			</form></td>
		</tr>
	</table>
	<% } %>
<hr />
<div id="visibility" >Visibility: <% out.print(visibility.toString()); %>.</div>
<div id="code" >
	<pre><code><p><% out.print(body); %></p></code></pre>
</div>
</body>
</html>